package me.learningandroid.ch2_7.uicomponentdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.HashSet;
import java.util.Set;

public class Splash1Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash1);

        Button showToastBtn = findViewById(R.id.showToastBtn);
        Button showYesNoDialogBtn = findViewById(R.id.showYesNoDialogBtn);
        Button showSingleChoiceDialogBtn = findViewById(R.id.showSingleChoiceDialogBtn);
        Button showMultipleChoiceDialogBtn = findViewById(R.id.showMultipleChoiceDialogBtn);
        FloatingActionButton nextBtn = findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener((view)-> {
            Splash2Activity.startActivity(this);
        });

        showToastBtn.setOnClickListener((view) -> {
            // 创建一个Toast对象。第一个参数时context，一般是所在的`Activity`，第二个参数是所显示的消息，第三个参数时显示时常。
            Toast toast = Toast.makeText(this,"这是一个Toast",Toast.LENGTH_LONG);
            // 可以给`Toast`注册一个回调函数，回调在`Toast`显示和隐藏式被调用。
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                toast.addCallback(new Toast.Callback() {
                    @Override
                    public void onToastShown() {
                        showToastBtn.setClickable(false);
                    }

                    @Override
                    public void onToastHidden() {
                        showToastBtn.setClickable(true);
                    }
                });
            }
            // `Toast.show()`显示出`Toast`
            toast.show();
        });
        showYesNoDialogBtn.setOnClickListener((view) -> {
            // 一个yes/no对话框，需要设置title，message，negativeButton和positiveButton。
            new AlertDialog.Builder(this)
                    .setTitle("是否对话框")
                    .setMessage("这是一个是否对话框")
                    .setCancelable(false)
                    .setNegativeButton("取消", (dialog,which) -> {
                        Toast.makeText(this,"选择了取消",Toast.LENGTH_LONG).show();
                    })
                    .setPositiveButton("确定", (dialog,which) -> {
                        Toast.makeText(this,"选择了确定",Toast.LENGTH_LONG).show();
                    })
                    .create()
                    .show();
        });
        showSingleChoiceDialogBtn.setOnClickListener((view) -> {
            // 一个yes/no对话框，需要设置title，message，negativeButton和positiveButton。
            new AlertDialog.Builder(this)
                    .setTitle("选择对话框")
                    .setItems(new String[]{"张三", "李四", "王五"}, (dialog, which)-> {
                        Toast.makeText(this,String.format("选择了%s", which),Toast.LENGTH_LONG).show();
                    })
                    .setCancelable(true)
                    .setOnCancelListener((dialog)-> {
                        Toast.makeText(this,"取消了对话框",Toast.LENGTH_LONG).show();
                    })
                    .create()
                    .show();
        });
        showMultipleChoiceDialogBtn.setOnClickListener((view) -> {
            Set<Integer> selected= new HashSet<>();
            new AlertDialog.Builder(this)
                    .setTitle("选择对话框")
                    .setMultiChoiceItems(new String[]{"张三", "李四", "王五"}, null, (dialog, which, s)-> {
                        Toast.makeText(this,String.format("选择了%s", which),Toast.LENGTH_LONG).show();
                        selected.add(which);
                    })
                    .setNegativeButton("取消", (dialog,which) -> {
                        Toast.makeText(this,"选择了取消",Toast.LENGTH_LONG).show();
                    })
                    .setPositiveButton("确定", (dialog,which) -> {
                        Toast.makeText(this,"选择了确定, 已选择："+selected,Toast.LENGTH_LONG).show();
                    })
                    .setCancelable(false)
                    .create()
                    .show();
        });
    }

    // 把menu资源inflate到系统菜单中
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // menu的回调，通过`MenuItem.getItemId()`获取到的组件id加以区分。
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==R.id.menuAbout) {
            Toast.makeText(this,"点击了关于菜单项",Toast.LENGTH_LONG).show();

        } else if (item.getItemId()==R.id.menuExit) {
            Toast.makeText(this,"点击了退出菜单项",Toast.LENGTH_LONG).show();
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        Log.i(getClass().getName(), "onBackPressed");
        Toast.makeText(this,"点击了系统的后退按钮",Toast.LENGTH_LONG).show();
        super.onBackPressed();
    }
}